package com.springboot.java;

import lombok.extern.slf4j.Slf4j;

import javax.swing.plaf.IconUIResource;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @Author: TongRui乀
 * @Date: 2020-03-08 14:22
 * @description： Java多线程测试
 */
@Slf4j
public class TestThread {

    private static int n = 0;

    private static Object lock = new Object();
    /**
     * 两个线程交替打印奇数和偶数
     */
    public static void test_01() throws InterruptedException {

        new Thread(()->func()).start();

        Thread.sleep(0);

        new Thread(()->func()).start();

    }

    public static void func() {

        while (n < 100){

            synchronized (lock){

                log.info("当前线程{}：当前count：{}", Thread.currentThread().getName(), n++);

                // 唤醒其它线程
                lock.notifyAll();

                if(n < 100){
                    //当前线程 退出对资源的监听 等待唤醒
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }

    /**
     *  三个线程交替打印 123
     */
    public static void test_02(){

        LinkedBlockingDeque<Integer> t = new LinkedBlockingDeque<>(3);

        t.add(1);
        t.add(2);
        t.add(3);

        t.getFirst();
    }

    public static void func2(){



    }


    public static void main(String[] args) throws InterruptedException {

        test_01();

        while (true){}

    }
}
